#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:taotieren


"""
    rpi.gpio 需要 root 权限，板上运行需要使用
    sudo python stepper.py
    控制单极电机
"""

""" python3 中请安装 tkintertoy 来进行程序调试和验证 """
from tkinter import *

""" 非树莓派环境下可以用 rpi.gpio_def 代替 rpi.gpio 来进行程序调试和验证 """
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
coil_A_1_pin = 18
coil_A_2_pin = 23
coil_B_1_pin = 24
coil_B_2_pin = 17

GPIO.setup(coil_A_1_pin, GPIO.OUT)
GPIO.setup(coil_A_2_pin, GPIO.OUT)
GPIO.setup(coil_B_1_pin, GPIO.OUT)
GPIO.setup(coil_B_2_pin, GPIO.OUT)

forward_seq = ['1001', '0110', '0101', '1001']
reverse_seq = list(forward_seq)  # to copy the list
reverse_seq.reverse()


def set_step(step):
    GPIO.output(coil_A_1_pin, step[0] == '1')
    GPIO.output(coil_A_2_pin, step[1] == '1')
    GPIO.output(coil_B_1_pin, step[2] == '1')
    GPIO.output(coil_B_2_pin, step[3] == '1')


def forward(delay, steps):
    for i in range(steps):
        for step in forward_seq:
            set_step(step)
            time.sleep(delay)


def backwards(delay, steps):
    for i in range(steps):
        for step in reverse_seq:
            set_step(step)
            time.sleep(delay)


while True:
    set_step('0000')
    delay = input("Delay betweeen steps (milliseconds (ms))?")
    steps = input("How many steps forward?")
    forward(int(delay) / 1000.0, int(steps))
    set_step('0000')
    steps = input("How many steps backwards?")
    backwards(int(delay) / 1000.0, int(steps))
